package kt_one.web.ktor

import io.ktor.application.*
import io.ktor.features.*
import io.ktor.gson.gson
import io.ktor.http.*
import io.ktor.request.uri
import io.ktor.response.*
import io.ktor.routing.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*

data class MyDataClass(val name: String, val message: String)

fun Application.module() {
    install(DefaultHeaders)
    install(CallLogging)
    install(ContentNegotiation) {
        gson {
            setPrettyPrinting()
        }
    }
    install(Routing) {
        get("/") {
            call.respondText("Request uri: ${call.request.uri}")
        }

        get("/test") {
            call.respondText("{\"hello\":1}", ContentType.Application.Json)
        }

        get("/json") {
            call.respond(MyDataClass("LiHua", "Good good study, day day up!"))
        }
    }
}

fun main(args: Array<String>) {
    embeddedServer(Netty, 8080, watchPaths = listOf("BlogAppKt"), module = Application::module).start()
}